package com.mjk.common.core.jdbc.bean;

import com.github.pagehelper.Page;

import com.mjk.common.base.model.BaseEntity;
import com.mjk.common.base.model.BaseExample;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.core.jdbc.util.provider.SqlProvider;
import com.mjk.common.tools.gson.GsonUtils;
import lombok.Data;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页包装类
 * 
 * @author mjk
 *
 */
@Data
public class BeePage<T extends BaseEntity> {
	private Integer pageNow;

	private Integer pageSize;

	private long pageTotal;

	private int pageNumber;
	
	private int pages;//总页数
	// 结果集
	private List<T> list;
	public BeePage (){
		this.pageNow = 1;
		this.pageSize = 15;
	}
	
	public BeePage (int pageStart,int pageSize){
		this.pageNow = pageStart;
		this.pageSize = pageSize;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public BeePage(List<T> list) {		
		if (list instanceof Page page) {
			this.pages = page.getPages();
			this.pageNow = page.getPageNum();
			this.pageSize = page.getPageSize();
			this.pageTotal = page.getTotal();
			this.pageNumber = page.getPages();			
			this.list =  page;
		}else if (list instanceof Collection) {
			this.pageNow = 1;
            this.pageSize = list.size();
            this.pageNumber = this.pageSize > 0 ? 1 : 0;
            this.list = list;
            this.pageSize = list.size();
            this.pageTotal = list.size();  
            if(this.pageSize ==0) {
            	this.pages = 0;
            }else {
            	this.pages = (int) ((this.pageTotal/this.pageSize==0)?this.pageTotal%this.pageSize:this.pageTotal%this.pageSize+1);
            }
		}
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public BeePage(List<?> list, BaseExample example) {
		if (list instanceof Page page) {
			this.pages = page.getPages();
			this.pageNow = page.getPageNum();
			this.pageSize = page.getPageSize();
			this.pageTotal = page.getTotal();
			this.pageNumber = page.getPages();
            this.list = (List<T>) SqlProvider.getListFromListMap(list, SqlProvider.getEntityFromExample(example));
		}else if (list instanceof Collection) {
			this.pageNow = 1;
            this.pageSize = list.size();
            this.pageNumber = this.pageSize > 0 ? 1 : 0;
            this.list = (List<T>) SqlProvider.getListFromListMap(list, SqlProvider.getEntityFromExample(example));
            this.pageSize = list.size();
            this.pageTotal = list.size();
            this.pages = (int) ((this.pageTotal/this.pageSize==0)?this.pageTotal%this.pageSize:this.pageTotal%this.pageSize+1);
		}
	}
	public BeePage(List<?> list, BeeQueryWrapper wrapper) {
		if (list instanceof Page page) {
			this.pages = page.getPages();
			this.pageNow = page.getPageNum();
			this.pageSize = page.getPageSize();
			this.pageTotal = page.getTotal();
			this.pageNumber = page.getPages();
            this.list = (List<T>) SqlProvider.getListFromListMap(list, wrapper.getInstance());
		}else if (list instanceof Collection) {
			this.pageNow = 1;
			this.pageSize = list.size();
			this.pageNumber = this.pageSize > 0 ? 1 : 0;
            this.list = (List<T>) SqlProvider.getListFromListMap(list, wrapper.getInstance());
			this.pageSize = list.size();
			this.pageTotal = list.size();
			this.pages = (int) ((this.pageTotal/this.pageSize==0)?this.pageTotal%this.pageSize:this.pageTotal%this.pageSize+1);
		}
	}
	public String toPageInfo(){
		Map<String,Object> result = new HashMap<>();
		result.put("pageNow", this.pageNow);
		result.put("pageSize", this.pageSize);
		result.put("pageTotal", this.pageTotal);
		result.put("pages", this.pages);
		return GsonUtils.bean2Json(result);
	}
	
	
}
